/*
 * @Author: 0x9DEFA478
 * @Date: 2024-07-29 17:34:34
 * @LastEditTime: 2024-08-11 20:35:34
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_TS_Lock.h"
#include "../LL/H_TS_LL.h"




void H_TS_Lock_Lock(H_TS_Lock* Lock){
  H_TS_Thread* thisThread;
  H_TS_Thread* Owner;
  H_TS_Thread* p_next;
  H_TS_Thread* p_next_next;
  H_TS_Thread* p_last;

  Hsize Priority;

  oH_TS_Assert_Core(H_TS_LL_Core_Instance.CurrentThread==NULL,"H_TS:H_TS_Lock_Lock():调度器未启动");
  oH_TS_Assert_HandleValid(Lock==NULL,"H_TS:H_TS_Lock_Lock():空的句柄");

  thisThread=cH_TS_LL_ThisThread(H_TS_LL_Core_Instance);

  oH_TS_LL_SchedulerSuspend();

  Owner=Lock->Owner;

  if(Owner==NULL){//无线程锁定这个锁, 锁定后返回
    
    Lock->Owner=thisThread;
    Lock->Priority=thisThread->Priority;//备份优先级

    oH_TS_LL_ResumeSchedulerAuto();
    return;
  }

  if(Owner==thisThread){//重复的锁定, 增加计数后返回
    Lock->ReLockCnt++;
    oH_TS_LL_ResumeSchedulerAuto();
    return;
  }

  //锁已被其他线程锁定

  thisThread->Status=vH_TS_Thread_Status_Block;//修改状态

  //从就绪链表里移除
  H_TS_LL_ThreadRemoteThisFromReadyList(thisThread,p_next,p_last);

  Priority=thisThread->Priority;

  //将thisThread添加到锁的阻塞链表中
  
  p_next=Lock->WaitLockThreads;
  if(p_next==NULL){
    //为空 添加到头部
    thisThread->List.Block.Last=NULL;
    thisThread->List.Block.Next=NULL;
    Lock->WaitLockThreads=thisThread;
  }else{
    //不为空 按优先级添加

    if(p_next->Priority>=Priority){
      //优先级最高 放置到开头
      thisThread->List.Block.Last=NULL;
      thisThread->List.Block.Next=p_next;
      p_next->List.Block.Last=thisThread;
      Lock->WaitLockThreads=thisThread;
    }else{
      //按优先级添加

      p_next_next=p_next->List.Block.Next;
      for(;;){
        if(p_next_next==NULL){
          //无插入点 添加到尾部

          thisThread->List.Block.Next=NULL;
          thisThread->List.Block.Last=p_next;
          p_next->List.Block.Next=thisThread;

          break;
        }

        p_next=p_next_next;
        if(p_next->Priority>=Priority){
          //找到插入点

          p_last=p_next->List.Block.Last;

          thisThread->List.Block.Next=p_next;
          thisThread->List.Block.Last=p_last;
          p_last->List.Block.Next=thisThread;
          p_next->List.Block.Last=thisThread;

          break;
        }
        p_next_next=p_next->List.Block.Next;
      }
    }
  }

  
  if(Owner->Priority>Priority){
    //优先级处理 需要提升线程Owner的优先级

    Owner->Priority=Priority;
    
    if(Owner->IsNotReady==0){
      //线程在就绪链表中 移除再添加来重排序

      //从就绪/副就绪链表里移除
      H_TS_LL_ThreadRemoteFromReadyList(Owner,p_next,p_last);

      //Owner线程优先级肯定是最高的
      //添加到就绪链表头部
      H_TS_LL_ThreadAddToReadyListHead(Owner,p_next,Priority);

    }

  }

  oH_TS_LL_ResumeScheduler();//线程调度

}

int H_TS_Lock_TryLock(H_TS_Lock* Lock){
  H_TS_Thread* thisThread;
  H_TS_Thread* Owner;
  int r;

  oH_TS_Assert_Core(H_TS_LL_Core_Instance.CurrentThread==NULL,"H_TS:H_TS_Lock_TryLock():调度器未启动");
  oH_TS_Assert_HandleValid(Lock==NULL,"H_TS:H_TS_Lock_TryLock():空的句柄");

  r=-1;

  thisThread=cH_TS_LL_ThisThread(H_TS_LL_Core_Instance);

  oH_TS_LL_SchedulerSuspend();

  Owner=Lock->Owner;

  if(Owner==NULL){//无线程锁定这个锁, 锁定后返回
    
    Lock->Owner=thisThread;
    Lock->Priority=thisThread->Priority;

    r=0;
    goto lReturn;
  }

  if(Owner==thisThread){//重复的锁定, 增加计数后返回

    Lock->ReLockCnt++;

    r=0;
    goto lReturn;
  }

  //锁已被其他线程锁定

lReturn:
  oH_TS_LL_ResumeSchedulerAuto();
  return r;
}

void H_TS_Lock_Unlock(H_TS_Lock* Lock){
  H_TS_Thread* thisThread;
  H_TS_Thread* p;
  H_TS_Thread* p_next;
  H_TS_Thread* p_last;
  Husize ReLockCnt;
  
  Hsize Priority;
  Hsize thisThreadPriority;
  int retVal;

  oH_TS_Assert_Core(H_TS_LL_Core_Instance.CurrentThread==NULL,"H_TS:H_TS_Lock_Unlock():调度器未启动");
  oH_TS_Assert_HandleValid(Lock==NULL,"H_TS:H_TS_Lock_Unlock():空的句柄");

  thisThread=cH_TS_LL_ThisThread(H_TS_LL_Core_Instance);

  oH_TS_Assert_Core(Lock->Owner!=thisThread,"H_TS:H_TS_Lock_Unlock():尝试解锁别的线程锁定的锁");

  ReLockCnt=Lock->ReLockCnt;
  if(ReLockCnt>0){
    Lock->ReLockCnt=ReLockCnt-1;
    return;
  }

  oH_TS_LL_SchedulerSuspend();

  p=Lock->WaitLockThreads;
  if(p==NULL){//无其他等待锁的线程
    Lock->Owner=NULL;
    
    oH_TS_Assert_Core(Lock->Priority!=thisThread->Priority,"H_TS:H_TS_Lock_Unlock():不应该出现的情况 等待线程里没有线程 并没有发生优先级继承");

    oH_TS_LL_ResumeSchedulerAuto();
    return;
  }

  Priority=Lock->Priority;
  thisThreadPriority=thisThread->Priority;

  oH_TS_Assert_Core(Priority<thisThreadPriority,"H_TS:H_TS_Lock_Unlock():不应该出现的情况 优先级继承机制只会把优先级提高 而现在优先级被降低了");
  
  thisThread->Priority=Priority;//恢复优先级
  if(thisThreadPriority<Priority){

    //优先级降低 移除再添加来重排序

    //从就绪链表里移除
    H_TS_LL_ThreadRemoteThisFromReadyList(thisThread,p_next,p_last);

    //添加到就绪链表
    H_TS_LL_ThreadAddToReadyList(thisThread,p_next,Priority,retVal);
    //并不要理会retVal 因为优先级继承机制 此时肯定有一个高优先级的线程因本锁阻塞 并且即将要解锁

  }
  
  p->Status=vH_TS_Thread_Status_Ready;//更新阻塞线程的状态
  Lock->Owner=p;//让新的线程锁定锁
  Lock->Priority=p->Priority;

  //从等待锁定链表中删除线程
  p_next=p->List.Block.Next;
  if(p_next!=NULL){
    p_next->List.Block.Last=NULL;
  }
  Lock->WaitLockThreads=p_next;

  //添加新锁定线程到就绪链表
  H_TS_LL_ThreadAddToReadyList(p,p_next,Priority,retVal);
  oH_TS_LL_ResumeSchedulerCondition(retVal);
  
}



